package com.baor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baor.util.R;
import com.baor.dao.ArticleDao;
import com.baor.domain.Article;
import com.baor.service.ArticleService;
import com.baor.util.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Service

public class ArticleServiceimpl implements ArticleService {
    @Autowired
    private  ArticleDao articleDao;
    @Autowired
    private UploadUtil uploadUtil;
    @Override
    public List<Article> abc() {       //进来查询所有
        List<Article> jg =articleDao.selectList(null);
        return jg;

    }

    @Override
    public R sx() {    //刷新
        List<Article> ss =articleDao.selectList(null);
        R a =R.ok(ss);
        return a;
    }

    @Override
    public boolean insert(Article article, MultipartFile[] imageFiles) {  //添加
        if (article.getId() == null) {                                    //编辑也可以用这个
            return false;
        }
        if (imageFiles != null && imageFiles.length > 0) {
            try {

                String imagePaths = uploadUtil.uploadUtil("/device/images", imageFiles);

                article.setCoverImage(imagePaths);
            } catch (Exception e) {

                return false;
            }
        }
        int row =articleDao.insert(article);
        return row > 0;
    }

    @Override
    public List<Article> find(String key) {  // 接收查询关键词
        QueryWrapper<Article> queryWrapper = new QueryWrapper<>();

        if (key != null && !key.isEmpty()) {
            queryWrapper.like("title", key);

        }


        List<Article> jg = articleDao.selectList(queryWrapper);
        return jg;
    }
}
